#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>

//#include "bad_spin_lock.c"
//#include "cas_spin_lock.c"
//#include "tas_spin_lock.c"
#include "fifo_spin_lock.c"
//#include "block_lock.c"

static lock_t printer_lock;

// 打印机
// 将字符串以单个字符循环输出
void my_print(char *str)
{
	lock(&printer_lock);
	while(*str!=0)
	{
		printf("%c", *str++);
		fflush(stdout);
		sleep(1);
	}
	printf("\n");
	unlock(&printer_lock);
}

void *print_word(void *arg)
{
	my_print((char *)arg);
	return NULL;
}


int main(int argc, char const *argv[])
{
	void *arg = NULL;
	// 1. 初始化锁
	init(&printer_lock);
	pthread_t tid, tid2, tid3, tid4;
	pthread_create(&tid, NULL, print_word, "11111111");
	pthread_create(&tid2, NULL, print_word, "22222222");
	pthread_create(&tid3, NULL, print_word, "33333333");
	pthread_create(&tid4, NULL, print_word, "44444444");	
	pthread_join(tid, &arg);//等待，阻塞
	pthread_join(tid2, &arg);//等待，阻塞
	pthread_join(tid3, &arg);//等待，阻塞
	pthread_join(tid4, &arg);//等待，阻塞
	return 0;
}
